/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : MqcpDeviceInfoReport.c
  * @brief          : MqcpDeviceInfoReport
  ******************************************************************************
  * @attention
  * 本文件用于  用户信息上报
  * @20210414:文件模板第一次生成
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */


#include "MqcpTemplate.h"

static MqcpDataDef *MqcpTemplateReqProcess(MqcpMessageDef *Message);
static MqcpDataDef *MqcpTemplateRespProcess(MqcpMessageDef *Message);

void TemplateInit(void)//用户信息上报初始化
{
	MqcpRouterDef routeReq = 
	{
		1,
		TemplateReq,
		MqcpTemplateReqProcess
	};
	while(addRouter(routeReq));
	
	MqcpRouterDef routeResp = 
	{
		1,
		TemplateResp,
		MqcpTemplateRespProcess
	};
	while(addRouter(routeResp));
}

MqcpDataDef *MqcpTemplateReqProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	MqcpData->isNeedResp = 0;
	MqcpData->Size = 0;
	
	uint16_t MessageSize = 0;
	
	rt_memset(MqcpData->MqcpMessage.MsgId, 0, 12);MqcpData->Size = MqcpData->Size + 12;
	MqcpData->MqcpMessage.Sync = MqcpSyncFalse;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Code = MqcpCodeOK;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Ver = MQCP_USE_VER;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.DevID[3] = MqcpDevice.DevId>>24;
	MqcpData->MqcpMessage.DevID[2] = MqcpDevice.DevId>>16;
	MqcpData->MqcpMessage.DevID[1] = MqcpDevice.DevId>>8;
	MqcpData->MqcpMessage.DevID[0] = MqcpDevice.DevId&0xff;MqcpData->Size = MqcpData->Size + 4;
	MqcpData->MqcpMessage.Cmd = TemplateReq;MqcpData->Size = MqcpData->Size + 1;
	
	MqcpData->MqcpMessage.Len[1] = MessageSize>>8;
	MqcpData->MqcpMessage.Len[0] = MessageSize&0xff;MqcpData->Size = MqcpData->Size + 2;
	
	MqcpData->MqcpMessage.Data[MessageSize] = Chack_Xor((uint8_t*)(&(MqcpData->MqcpMessage)),MqcpData->Size);MqcpData->Size = MqcpData->Size + 1;
	
	return MqcpData;
}

MqcpDataDef *MqcpTemplateRespProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	MqcpData->isNeedResp = Message->Sync;
	MqcpData->Size = 0;
	
	uint16_t ResourcesSize = 0;
	uint16_t MessageSize = 0;
	
	uint16_t D2Bytei = 0;
	uint16_t D4Bytei = 0;
	
	ResourcesSize = (Message->Data[MessageSize + 1]<<8)|Message->Data[MessageSize];MessageSize = MessageSize + 2;
	
	for(uint16_t i = 0; i < ResourcesSize; i++)
	{
		if((MqcpInt32 == Message->Data[MessageSize])||(MqcpUint32 == Message->Data[MessageSize])||(MqcpFloat32 == Message->Data[MessageSize]))
		{
			Data4Byte[D4Bytei].Type = Message->Data[MessageSize];
			Data4Byte[D4Bytei].Address = (Message->Data[MessageSize + 2]<<8)|Message->Data[MessageSize + 1];
			Data4Byte[D4Bytei].Data[0] = AddrGetValue(Data4Byte[D4Bytei].Address - 1);
			Data4Byte[D4Bytei].Data[1] = AddrGetValue(Data4Byte[D4Bytei].Address - 1 +1);
			
			D4Bytei++;
		}
		else
		{
			Data2Byte[D2Bytei].Type = Message->Data[MessageSize];
			Data2Byte[D2Bytei].Address = (Message->Data[MessageSize + 2]<<8)|Message->Data[MessageSize + 1];
			Data2Byte[D2Bytei].Data = AddrGetValue(Data2Byte[D2Bytei].Address - 1);
			
			D2Bytei++;
		}
		MessageSize = MessageSize + 3;
	}
	
	return MqcpData;
}

/************************ (C) COPYRIGHT CunJu *****END OF FILE****/
